
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib import font_manager


def template(x, y):
    fig, ax = plt.subplots(figsize=(20, 8), dpi=80)
    my_font = font_manager.FontProperties(
        fname="/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc")

    _x = range(len(x))
    ax.bar(_x, y)
    ax.set_title("Title", fontsize=18)
    ax.set_xlabel("Xlabel",
                  fontsize=18,
                  fontfamily='sans-serif',
                  fontstyle="italic")
    ax.set_ylabel("ylablel", fontsize='x-large', fontstyle="oblique")
    ax.legend()  # 图例

    # ax.set_aspect("equal")  # 长宽一样
    ax.minorticks_on()
    ax.set_xlim(0, 16)
    ax.grid(which="minor", axis='both')

    ax.xaxis.set_tick_params(rotation=45, labelsize=18, colors='g')  # 调整字体样式
    start, end = ax.get_xlim()  # 获取X 轴 的最大值和最小值
    ax.xaxis.set_ticks(_x)  # 设置x轴数字起始
    ax.yaxis.tick_right()  # Y 轴移动到右边

    plt.show()


def run_start():
    path = "/home/lq/文档/123.xlsx"
    df = pd.read_excel(path, sheet_name=0)
    # dict_df = df.groupby(by="position")
    ds = (df.groupby(by="position").count()["Unnamed: 0"])
    # ds = pd.Series(dtype=float)
    # for i, j in dict_df:
    #     j = len(j)
    #     ds[i] = j

    print(ds)

    # plot_tu(ds)


def plot_tu(ds):
    # 画布基础
    fig, ax = plt.subplots(figsize=(20, 8), dpi=80)
    # plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
    # plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
    my_font = font_manager.FontProperties(
        fname="/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc")
    # 画图
    _x = [i + 0.5 for i in range(len(ds.index))]
    ax.bar(_x, ds.values, width=0.5, label="设备")
    # title
    ax.set_xlabel("车间", fontproperties=my_font, fontsize=20)
    ax.set_ylabel("数量", fontproperties=my_font, fontsize=20)
    ax.set_title("车间设备数量", fontproperties=my_font, fontsize=30)
    # 设置X轴
    ax.set_xticks(_x)
    ax.set_xticklabels(list(ds.index), fontproperties=my_font)
    # y
    ax.set_yticks(range(ds.max())[::30])
    # 网格
    # ax.grid(which="minor", linestyle='--', alpha=0.8)
    ax.grid(which="minor", axis='both')
    ax.legend(prop=my_font)
    plt.show()


run_start()
